/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.spring.tracing;

import com.jakewharton.nopen.annotation.Open;

import io.sentry.IHub;

import org.aopalliance.aop.Advice;
import org.jetbrains.annotations.NotNull;
import org.springframework.aop.Advisor;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Creates advice infrastructure for {@link SentrySpan} and {@link SentryTransaction}.
 */
@Configuration(proxyBeanMethods = false)
@Open
public class SentryAdviceConfiguration {
    /**
     * sentryTransactionAdvice
     *
     * @param hub IHub
     * @return Advice
     */
    @Bean
    public @NotNull Advice sentryTransactionAdvice(final @NotNull IHub hub) {
        return new SentryTransactionAdvice(hub);
    }

    /**
     * sentryTransactionAdvisor
     *
     * @param sentryTransactionPointcut Pointcut
     * @param sentryTransactionAdvice Advice
     * @return Advisor
     */
    @Bean
    public @NotNull Advisor sentryTransactionAdvisor(
        final @NotNull @Qualifier("sentryTransactionPointcut") Pointcut sentryTransactionPointcut,
        final @NotNull @Qualifier("sentryTransactionAdvice") Advice sentryTransactionAdvice) {
        return new DefaultPointcutAdvisor(sentryTransactionPointcut, sentryTransactionAdvice);
    }

    /**
     * sentrySpanAdvice
     *
     * @param hub IHub
     * @return Advice
     */
    @Bean
    public @NotNull Advice sentrySpanAdvice(final @NotNull IHub hub) {
        return new SentrySpanAdvice(hub);
    }

    /**
     * sentrySpanAdvisor
     *
     * @param sentrySpanPointcut Pointcut
     * @param sentrySpanAdvice Advice
     * @return Advisor
     */
    @Bean
    public @NotNull Advisor sentrySpanAdvisor(
        final @NotNull @Qualifier("sentrySpanPointcut") Pointcut sentrySpanPointcut,
        final @NotNull @Qualifier("sentrySpanAdvice") Advice sentrySpanAdvice) {
        return new DefaultPointcutAdvisor(sentrySpanPointcut, sentrySpanAdvice);
    }
}
